package controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BookingDTO;
import model.BookingSearchDTO;
import model.UserDTO;
import bizlogic.BookingManager;
import bizlogic.UserManager;

/**
 * Servlet implementation class ViewPendingBooking
 */
@WebServlet("/ViewPendingBooking")
public class ViewPendingBookingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewPendingBookingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession hs = request.getSession(true);
		UserDTO user = (UserDTO)hs.getAttribute("user");
		user = (UserDTO)hs.getAttribute("user");

		
		BookingManager bm=new BookingManager();
		UserManager um = new UserManager();
		ArrayList<BookingDTO> Targetbookings=new ArrayList<BookingDTO>();
		//ArrayList<UserDTO> ul = um.findAllUsers();
		UserDTO[] ul = um.findAllUsers();
		
		for(UserDTO u : ul){
			if((u.getManager().matches(user.getUsername()))||u.getUsername().matches(user.getUsername())){
				BookingSearchDTO bso = new BookingSearchDTO();
				bso.setUserId(u.getUsername());
				ArrayList<BookingDTO> b = bm.findBookingsByCriteria(bso);
				for(BookingDTO bd : b)
				{
					if(bd.getStatus().matches("Pending"))
					{
						Targetbookings.add(bd);
					}
				}
			}
		}
		request.setAttribute("booking", Targetbookings);
		RequestDispatcher rd = request.getRequestDispatcher("ViewPendingBooking.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
